import loading from '@/components/xuan-loading/xuan-loading.vue'
import cLabel from "@/components/cat-label/cat-label.vue"
import fromItem from '@/components/from-item/from-item.vue'
import wx from "weixin-js-sdk";
import {
	log
} from 'util'
export default {
	props: {
		mustIcon: {
			type: String,
			default: require('@/static/Scope.png'),
		},
		unIcon: {
			type: String,
			default: require('@/static/unscope.png'),
		}
	},
	data() {
		return {
			Inv: 0,
			positionData: [], //存放位置
			page: false,
			click: '',
			appliance: null,
			num:3,

			newDisabled: false, //基本信息是否可编辑
			newMust: "must", //基本信息必填
			flagNum: -1,
			// check:false,//处理是否可编辑
			gridmanList: [], //所有网格员
			selector: "text",
			date1: "date1",
			// 弹框
			selectParent: true,
			multiple: false,
			companyTree: [], //树结构	
			TreeNum: null, //第几个树结构
			flod: true,

			// loading样式
			width: '150rpx',
			height: '100rpx',
			shadeShow: false,
			backgroundColor: "transparent",
			themeColor: '#33CCCC',
			// loading样式
			formData: {
				fId: "", //
				fWarehousingdates: "", //入库日期
				fPosition: "", //存放位置
				fPositionid: "", //存放位置id
				fWarehousingusername: "", //入库人员
				fWarehousinguserid: "", //入库人员id
				userId: "", //当前登录人id
				userName: "", //当前登录人姓名
				isSubmit: "", //0保存1提交
				itemsList: []
			},
			totalData: {}, // 详情


			userInfo: null,
			must: "must",
			disabled: false,
			recordfiles: [], //删除文件集合
			optionsF: null, //上个页面传递过来的参数
			timing: null, //当前年月日
			//显示隐藏
			basic: 1, //基本信息
			close: 1, //问题处理情况
			//显示隐藏
			// index: 0,
			multiArray: [
				['亚洲', '欧洲'],
				['中国', '日本'],
				// ['北京', '上海', '广州']
			],
			multiIndex: [0, 0, 0],
			fWorktypeindex: 0,
			postionArr: null,
		};
	},
	onLoad(options) {
this.digui(this.num)
		var that = this;
		uni.getStorage({ //监督检查方式
			key: 'userInfo',
			success: function(res) {
				that.userInfo = JSON.parse(res.data);
				// console.log("that.userInfo", that.userInfo);
				that.formData.fWarehousingusername = that.userInfo.realName //入库人员
				that.formData.fWarehousinguserid = that.userInfo.userId //入库人员id
				that.formData.userName = that.userInfo.realName //入库人员
				that.formData.userId = that.userInfo.userId //入库人员id
				that.optionsF = options;
				that.formData.fId = options.id
				that.getDetailData(options.id)
				that.getPostion(that.userInfo.companyId)
			}
		});



		// || MP-WEIXIN || H5
		// #ifdef APP-PLUS
		this.playVideo()
		uni.$on('drawer-page', (data) => {
			that.clickNum(data)
		})
		// #endif
		// #ifdef H5
		this.appliance = 1
		// that.geterweimaInfo()
		// #endif

	},


	onShow() {

	},
	components: {
		loading,
		cLabel,

		fromItem,


	},

	onUnload() {
		uni.$off('drawer-page')
	},
	onNavigationBarButtonTap(e) {


		var that = this

		if (e.index === 0) {
			console.log(e)
			this.geterweimaInfo()
		}

	},
	methods: {
		digui(num){
			var that=this
			if (num==0) return false;
			console.log('第'+num+'次',num)
			that.timeId = setTimeout(() => {
				num--
				that.digui(num)
			}, 1500)
		},
		geterweimaInfo() {
			var that = this

			var url = encodeURIComponent(window.location.href.split("#")[0]);
			this.$http.checkToken({
				url: url
			}).then(res => {
				let data = res.data;
				console.log(data)
				wx.config({
					debug: false,
					appId: data.appId,
					timestamp: data.timestamp,
					nonceStr: data.nonceStr,
					signature: data.signature,
					jsApiList: ["checkJsApi", "openLocation","scanQRCode"],
					success(res) {
						console.log('nihao', res)
					}
				})
				wx.ready(function() {
					wx.checkJsApi({ //判断当前客户端版本是否支持指定JS接口
						jsApiList: ['scanQRCode'],
						success: function(
						res) { // 以键值对的形式返回，可用true，不可用false。如：{"checkResult":{"scanQRCode":true},"errMsg":"checkJsApi:ok"}
							if (res.checkResult.scanQRCode != true) {

								// uni.showToast({
								// 	title: "抱歉，当前客户端版本不支持扫一扫",
								// 	icon: 'none',
								// 	duration: 2000
								// });
							}else {
								wx.scanQRCode({
									needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
									scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
									success: function(res) {
										var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
										// uni.showToast({
										// 	title: "微信调用扫一扫接口成功" + JSON.stringify(result),
										// 	icon: 'none',
										// 	duration: 20000
										// });
										that.formData.itemsList.map(item => {
											if (item.fSampleitems == result) {
												item.fWarehousingitems = result
												item.status = 1 //正在入库
											}
										})
										// that.$forceUpdate()
										that.timeId = setTimeout(() => {
											that.geterweimaInfo()
										}, 1500)
									},
									error: function(res) {
										console.log(res)
										if (res.errMsg.indexOf('function_not_exist') > 0) {
								
											// uni.showToast({
											// 	title: "版本过低请升级",
											// 	icon: 'none',
											// 	duration: 2000
											// });
										}
									}
								
								
								})
							}
						},
						fail: function(res) { //检测getNetworkType该功能失败时处理
							alert('checkJsApi error');
						}
					}); //wx.checkJsApi结束



				
				})
				wx.error(function(res) {
					uni.showToast({
						title: "微信调用扫一扫接口报错" + JSON.stringify(res),
						icon: 'none',
						duration: 2000
					});
					// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
				});



			})
		},
		bindMultiPickerColumnChange: function(e) {
			this.multiIndex[e.detail.column] = e.detail.value
			if (e.detail.column == 0) {
				this.positionData[1] = this.postionArr[e.detail.value].curingList
				this.positionData[2] = this.postionArr[e.detail.value].curingList[0].layerList
				this.multiIndex.splice(1, 1, 0)
				this.multiIndex.splice(2, 1, 0)
			} else if (e.detail.column == 1) {
				this.positionData[2] = this.positionData[1][e.detail.value].layerList
				this.multiIndex.splice(2, 1, 0)
			}
			
			this.formData.fPosition = this.postionArr[this.multiIndex[0]].fCuringno + '-' + this.postionArr[this
					.multiIndex[0]].curingList[this.multiIndex[1]].fCuringno + '-' + this.postionArr[this.multiIndex[0]]
				.curingList[this.multiIndex[1]].layerList[this.multiIndex[2]].fCuringno
			this.$forceUpdate()
		},

		submit() {
			var that = this
			this.Inv = 1

		},
		unsubmit() {
			var that = this
			this.Inv = 0
		},


		clickNum(data) {
			this.saveData(data);
		},


		getNewData() {
			var that = this;
		},
		// 详情
		getDetailData(id) {
			this.getDate();
			var that = this;
			that.fileList = [];
			var datas = {
				fId: id,
			}
			that.$http.getSampleConserveByFid(datas).then(res => {
				if (res.errcode == 0) {
					that.totalData = res.data.yjQmpYpCheckSampleconserve;
					res.data.yjQmpYpCheckSampleconserve.itemsList.map(item => {
						if (item.fWarehousingitems != '') {
							item['status'] = 0 //已入库
						}
					})
					if(that.totalData.fPosition==''||that.totalData.fPosition==null){
					that.fWorktypeindex=-1	
					}else {
						
						that.fWorktypeindex=0
				
						that.formData.fWarehousingdates=that.totalData.fWarehousingdates //入库日期
						that.formData.fPosition=that.totalData.fPosition //存放位置
						that.formData.fPositionid=that.totalData.fPositionid //存放位置id
						that.formData.itemsList=that.totalData.itemsList
					}
					
					that.formData.itemsList = res.data.yjQmpYpCheckSampleconserve.itemsList
					that.page = true
					console.log(that.formData)

				} else {
					uni.showToast({
						title: "请求失败",
						icon: 'none',
						duration: 2000
					});
				}

			}, (err) => {
				uni.showToast({
					title: "接口报错",
					icon: 'none',
					duration: 2000
				});
			})

		},

		// 下拉框
		selectorFun(e) {
			console.log("下拉选择", e);

		},
		// 获取存放位置
		getPostion(id) {
			this.positionData = [];
			var data = {
				page: 1,
				pageSize: 800,
				fCompanyid: id, //單位id
				keyWord: "" //养护室编号或名称
			};
			

			this.$http.getCuringlocationList(data)
				.then((res) => {
					if (res.errcode == 0) {
						var arr = []
						this.postionArr = res.data.pageInfo.list;
						this.positionData = [res.data.pageInfo.list, res.data.pageInfo.list[0].curingList, res.data
							.pageInfo.list[0].curingList[0].layerList
						];

					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
						// this.$refs.loading.close();
					}
				}, (err) => {
					uni.showToast({
						title: err.msg,
						icon: 'none',
						duration: 2000
					});
					// this.$refs.loading.close();
				});
		},
		// 获取指定网格员

		// 判断必填项
		saveData(data) {
			this.formData.fPosition = this.postionArr[this.multiIndex[0]].fCuringno + '-' + this.postionArr[this
					.multiIndex[0]].curingList[this.multiIndex[1]].fCuringno + '-' + this.postionArr[this.multiIndex[0]]
				.curingList[this.multiIndex[1]].layerList[this.multiIndex[2]].fCuringno
			this.formData.fPositionid = this.postionArr[this.multiIndex[0]].curingList[this.multiIndex[1]].layerList[
				this.multiIndex[2]].fId //存放位置id
			var that = this;
			console.log(this.formData, data)
			that.formData.isSubmit = data.toString()
			// uni.showToast({
			// 	title: JSON.stringify(that.formData),
			// 	icon: 'none',
			// 	duration: 20000
			// });
			// return false
			that.$refs.loading.open();
			this.$http.saveSampleConserve(this.formData).then(res => {
				if (res.errcode == 0) {
					// that.$refs.loading.close();
					that.page = false
					if (!that.click) {
						setTimeout(() => {
							if (data == 0) {
								uni.showToast({
									title: "保存成功",
									icon: 'none',
									duration: 2000
								});
							} else if (data == 1) {
								uni.showToast({
									title: "提交成功",
									icon: 'none',
									duration: 2000
								});
							}
							uni.redirectTo({
								url: "/pages/apply/softFoundationManage/overLimitMonitor/testing/index?Inv=" +
									data,
								success: () => {
									that.clickN = 1
									that.click = false
									that.$refs.loading.close();
									// #ifdef H5
									setTimeout(() => {
										that.click = false
									}, 100)
									// #endif
								}
							})
						}, 1500)
					}

					that.click = true

				} else {
					console.log("接口报错")
					that.$refs.loading.close();
					that.click = true
				}
			}, (err) => {
				that.$refs.loading.close();
				that.click = true
				uni.showToast({
					title: "请求失败",
					icon: 'none',
					duration: 2000
				});
			})


		},


		// 原生窗体按钮显示
		playVideo() {
			let subNVue = uni.getSubNVueById('safe_notice')
			subNVue.show()
		},

		// 获取当前日期
		getDate() {
			var time = new Date();
			const y = time.getFullYear();
			const m = (time.getMonth() + 1 + "").padStart(2, "0");
			const d = (time.getDate() + "").padStart(2, "0");
			const hh = (time.getHours() + "").padStart(2, "0");
			const mm = (time.getMinutes() + "").padStart(2, "0");
			const ss = (time.getSeconds() + "").padStart(2, "0");

			var timer = `${y}-${m}-${d} ${hh}:${mm}`;
			var timing = `${y}-${m}-${d}`;

			this.formData.fWarehousingdates = `${y}-${m}-${d}`;

		},
		callback() {}
	}
}
